package com.hotel.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.hotel.bean.Room;
import com.hotel.bean.User;
import com.hotel.exception.SteduException;
import com.hotel.mapper.OrderMapper;
import com.hotel.mapper.RoomMapper;
import com.hotel.mapper.UserMapper;
import com.hotel.service.UserService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insert(User user) throws SteduException {
        if (userMapper.selectByUnumber(user.getUnumber()) != null) {
            throw new SteduException("身份证输入错误,已存在");
        }
        //添加用户
        userMapper.insert(user);
        //修改房间状态
        roomMapper.updateRstate(user.getRnumber(),2);
        //修改关联第三张表
        Integer rid = roomMapper.selectByRnumber(user.getRnumber()).getRid();
        Integer uid = userMapper.selectByUnumber(user.getUnumber()).getUid();
        //删除预定房间
        orderMapper.deleteOidAndRidByRid(roomMapper.selectByRnumber(user.getRnumber()).getRid());
        return userMapper.insertRoom(rid, uid) == 1;
    }
    @Override
    public boolean update(User user) throws SteduException {
        if (userMapper.selectByUid(user.getUid()) == null) {
            throw new SteduException("用户不存在");
        }
        User user1 = userMapper.selectByUnumber(user.getUnumber());
        if (user1 != null && user1.getUid() != user.getUid()) {
            throw new SteduException("身份证重复");
        }
        return userMapper.update(user) == 1;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateUstate(Integer uid) throws SteduException {
        if (userMapper.selectByUid(uid) == null) {
            throw new SteduException("用户不存在");
        }
        User user = userMapper.selectByUid(uid);
        String rnumber = user.getRnumber();
        Room room = roomMapper.selectByRnumber(rnumber);
        //删除第三张表联系
        roomMapper.deleteRidAndUidByRid(room.getRid());
        //修改用户状态为已退房
        userMapper.updateUstate(uid);
        //讲房间状态修改为打扫维护
        return roomMapper.updateRstate(rnumber,3)==1;
    }
    @Override
    public User selectByUid(Integer uid) {
        return userMapper.selectByUid(uid);
    }
    @Override
    public List<User> selectAll(String uname) {
        return userMapper.selectAll(uname);
    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        List<User> users = userMapper.selectAll(null);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(null, "用户表表"), User.class, users);
        // 输出
        responseWorkbook(response, workbook, "用户信息导出.xlsx");
    }
    /**
     * 导出 Excel
     *
     * @param response
     * @param workbook
     * @param fileName
     */
    private static void responseWorkbook(HttpServletResponse response, Workbook workbook, String fileName) {

        try {
            response.setHeader("Content-disposition", "attachment;" + "filename*=utf-8''" + URLEncoder.encode(fileName, "UTF-8"));
            response.setContentType("application/octet-stream; charset=UTF-8");
            workbook.write(response.getOutputStream());

            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
